home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / system-config-printer / probe_printer.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-10-28  |  7KB  |  292 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import cupshelpers
  5. from debug import *
  6. import socket
  7. import time
  8. import gtk
  9. from timedops import TimedOperation
  10. import subprocess
  11.  
  12. def wordsep(line):
  13.     words = []
  14.     escaped = False
  15.     quoted = False
  16.     in_word = False
  17.     word = ''
  18.     n = len(line)
  19.     for i in range(n):
  20.         ch = line[i]
  21.         if escaped:
  22.             word += ch
  23.             escaped = False
  24.             continue
  25.         
  26.         if ch == '\\':
  27.             in_word = True
  28.             escaped = True
  29.             continue
  30.         
  31.         if in_word:
  32.             if quoted:
  33.                 if ch == '"':
  34.                     quoted = False
  35.                 else:
  36.                     word += ch
  37.             elif ch.isspace():
  38.                 words.append(word)
  39.                 word = ''
  40.                 in_word = False
  41.             elif ch == '"':
  42.                 quoted = True
  43.             else:
  44.                 word += ch
  45.         quoted
  46.         if ch == '"':
  47.             in_word = True
  48.             quoted = True
  49.             continue
  50.         if not ch.isspace():
  51.             in_word = True
  52.             word += ch
  53.             continue
  54.     
  55.     if word != '':
  56.         words.append(word)
  57.     
  58.     return words
  59.  
  60.  
  61. class LpdServer:
  62.     
  63.     def __init__(self, hostname):
  64.         self.hostname = hostname
  65.         self.max_lpt_com = 8
  66.  
  67.     
  68.     def _open_socket(self):
  69.         port = 515
  70.         
  71.         try:
  72.             (host, port) = self.hostname.split(':', 1)
  73.         except ValueError:
  74.             host = self.hostname
  75.  
  76.         s = None
  77.         
  78.         try:
  79.             ai = socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM)
  80.         except socket.gaierror:
  81.             ai = []
  82.  
  83.         for res in ai:
  84.             (af, socktype, proto, canonname, sa) = res
  85.             
  86.             try:
  87.                 s = socket.socket(af, socktype, proto)
  88.                 s.settimeout(0.5)
  89.             except socket.error:
  90.                 msg = None
  91.                 s = None
  92.                 continue
  93.  
  94.             
  95.             try:
  96.                 s.connect(sa)
  97.             except socket.error:
  98.                 msg = None
  99.                 s.close()
  100.                 s = None
  101.                 continue
  102.  
  103.             break
  104.         
  105.         return s
  106.  
  107.     
  108.     def probe_queue(self, name, result):
  109.         s = self._open_socket()
  110.         if not s:
  111.             return False
  112.         print name
  113.         
  114.         try:
  115.             s.send('\x02%s\n' % name)
  116.             data = s.recv(1024)
  117.             print repr(data)
  118.         except socket.error:
  119.             msg = None
  120.             print msg
  121.             
  122.             try:
  123.                 s.close()
  124.             except:
  125.                 pass
  126.  
  127.             return False
  128.  
  129.         if len(data) > 0 and ord(data[0]) == 0:
  130.             
  131.             try:
  132.                 s.send('\x01\n')
  133.                 s.close()
  134.             except:
  135.                 pass
  136.  
  137.             result.append(name)
  138.             return True
  139.         
  140.         try:
  141.             s.close()
  142.         except:
  143.             ord(data[0]) == 0
  144.  
  145.         return False
  146.  
  147.     
  148.     def get_possible_queue_names(self):
  149.         candidate = [
  150.             'PASSTHRU',
  151.             'ps',
  152.             'lp',
  153.             'PORT1',
  154.             '']
  155.         for nr in range(self.max_lpt_com):
  156.             candidate.extend([
  157.                 'LPT%d' % nr,
  158.                 'LPT%d_PASSTHRU' % nr,
  159.                 'COM%d' % nr,
  160.                 'COM%d_PASSTHRU' % nr])
  161.         
  162.         for nr in range(50):
  163.             candidate.append('pr%d' % nr)
  164.         
  165.         return candidate
  166.  
  167.     
  168.     def probe(self):
  169.         result = []
  170.         for name in self.get_possible_queue_names():
  171.             while gtk.events_pending():
  172.                 gtk.main_iteration()
  173.             found = self.probe_queue(name, result)
  174.             if not found and name.startswith('pr'):
  175.                 break
  176.             
  177.             time.sleep(0.1)
  178.         
  179.         return result
  180.  
  181.  
  182.  
  183. class PrinterFinder:
  184.     
  185.     def find(self, hostname, callback_fn):
  186.         self.hostname = hostname
  187.         self.callback_fn = callback_fn
  188.         op = TimedOperation(self._do_find)
  189.  
  190.     
  191.     def _do_find(self):
  192.         self._cached_attributes = dict()
  193.         for fn in [
  194.             self._probe_snmp,
  195.             self._probe_lpd,
  196.             self._probe_hplip]:
  197.             
  198.             try:
  199.                 fn()
  200.             continue
  201.             except Exception:
  202.                 e = None
  203.                 nonfatalException()
  204.                 continue
  205.             
  206.  
  207.         
  208.         self.callback_fn(None)
  209.  
  210.     
  211.     def _probe_snmp(self):
  212.         null = file('/dev/null', 'r+')
  213.         
  214.         try:
  215.             p = subprocess.Popen(args = [
  216.                 '/usr/lib/cups/backend/snmp',
  217.                 self.hostname], stdin = null, stdout = subprocess.PIPE, stderr = null)
  218.         except OSError:
  219.             e = None
  220.             if e == errno.ENOENT:
  221.                 return None
  222.             raise 
  223.         except:
  224.             e == errno.ENOENT
  225.  
  226.         (stdout, stderr) = p.communicate()
  227.         if p.returncode != 0:
  228.             return None
  229.         for line in stdout.split('\n'):
  230.             words = wordsep(line)
  231.             n = len(words)
  232.             if n == 5:
  233.                 (device_class, uri, make_and_model, info, device_id) = words
  234.             elif n == 4:
  235.                 (device_class, uri, make_and_model, info) = words
  236.             
  237.             device_dict = {
  238.                 'device-class': device_class,
  239.                 'device-make-and-model': make_and_model,
  240.                 'device-info': info }
  241.             if n == 5:
  242.                 device_dict['device-id'] = device_id
  243.             
  244.             device = cupshelpers.Device(uri, **device_dict)
  245.             self.callback_fn(device)
  246.             self._cached_attributes['device-make-and-model'] = make_and_model
  247.         
  248.  
  249.     
  250.     def _probe_lpd(self):
  251.         lpd = LpdServer(self.hostname)
  252.         for name in lpd.get_possible_queue_names():
  253.             found = lpd.probe_queue(name, [])
  254.             if found:
  255.                 uri = 'lpd://%s/%s' % (self.hostname, name)
  256.                 device_dict = {
  257.                     'device-class': 'network',
  258.                     'device-info': self.hostname }
  259.                 device_dict.update(self._cached_attributes)
  260.                 new_device = cupshelpers.Device(uri, **device_dict)
  261.                 self.callback_fn(new_device)
  262.             
  263.             if not found and name.startswith('pr'):
  264.                 break
  265.             
  266.             time.sleep(0.1)
  267.         
  268.  
  269.     
  270.     def _probe_hplip(self):
  271.         null = file('/dev/null', 'r+')
  272.         
  273.         try:
  274.             p = subprocess.Popen(args = [
  275.                 'hp-makeuri',
  276.                 '-c',
  277.                 self.hostname], stdin = null, stdout = subprocess.PIPE, stderr = null)
  278.         except OSError:
  279.             e = None
  280.             if e == errno.ENOENT:
  281.                 return None
  282.             raise 
  283.         except:
  284.             e == errno.ENOENT
  285.  
  286.         (stdout, stderr) = p.communicate()
  287.         if p.returncode != 0:
  288.             return None
  289.         uri = stdout.strip()
  290.  
  291.  
  292.